'''
结账
'''
import unittest
from common.Base import Base, open_browser
from Page.shopping_process_page import shopping_process
from common.databaseTool import DatabaseTool
from selenium.webdriver.common.by import By
import ddt
from common.openExcel import Open_excel
import os
from Page.shoppingcart_page import Shopingcart_page
from time import sleep
import hashlib

# 登录页面url
url = "http://localhost:8080/ecshop/user.php"

class Shopingcart_case(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.data = DatabaseTool("168168", "ecshop_db", port=3308)
        sql = f"insert into ecs_users (user_id,user_name,password) values (20,'test','{hashlib.md5(b'111111').hexdigest()}')"
        cls.data.execute_one(sql)
        sql2 ="INSERT INTO `ecshop_db`.`ecs_user_address` (`address_id`, `address_name`, `user_id`, `consignee`, `email`, `country`, `province`, `city`, `district`, `address`, `zipcode`, `tel`, `mobile`, `sign_building`, `best_time`) VALUES ('20', '', '20', 'qioqio', '1312311@qq.com', '1', '2', '37', '404', '啊这', '', '1312312331', '', '', '')"
        cls.data.execute_one(sql2)

    @classmethod
    def tearDownClass(cls):
        sql = "delete from ecs_users where user_name like '%test%'"
        cls.data.execute_one(sql)
        sql2 = "DELETE FROM `ecs_user_address` WHERE user_id ='20'"
        cls.data.execute_one(sql2)


    def setUp(self):
        # 每一个用例执行前执行 准备测试数据
        op = open_browser()
        self.shop = shopping_process(op)
        self.shop.open_url(url)
        self.shop.input_username("test")
        self.shop.input_password("111111")
        self.shop.click_submit()
        self.shop.click_homepage()

    def tearDown(self):
        pass
        # 每一个用例执行后执行 清除环境
        # 关闭浏览器
        # self.shop.quit()
    # def test_shopping_invoicing_047(self):
    #     # 点击视频商品
    #     self.shop.click_video()
    #     # 点击加入购物车
    #     self.shop.click_goods_confirm()
    #     # 点击结账
    #     self.shop.click_goods_settlement()

    #     #断言
    #     try:
    #         self.assertEqual(self.shop.select_goods("视频"),"视频",msg="用例执行失败未跳转到正确页面")
    #     except:
    #         pass
    # def test_shopping_invoicing_048(self):
    #     # 点击视频商品
    #     self.shop.click_video()
    #     # 点击加入购物车
    #     self.shop.click_goods_confirm()
    #     #点击结账
    #     self.shop.click_goods_settlement()

    #     #断言
    #     try:
    #         # 点击修改收货地址
    #         self.shop.update_addr()
    #         self.assertEqual(self.shop.select_goods("收货人信息"), "收货人信息", msg="用例执行失败为跳转正确页面")
    #     except:
    #         pass

    def test_shopping_invoicing_049(self):
        # 点击视频商品
        self.shop.click_video()
        # 点击加入购物车
        self.shop.click_goods_confirm()
        #点击结账
        self.shop.click_goods_settlement()
        #断言
        try:
            # 点击确定
            self.shop.click_invoicing()
            self.shop.click_window()
            self.assertTrue(True)
        except:
            self.assertTrue(False)

    def test_shopping_invoicing_051(self):
        # 点击视频商品
        self.shop.click_video()
        # 点击加入购物车
        self.shop.click_goods_confirm()
        #点击结账
        self.shop.click_goods_settlement()
        self.shop.click((By.XPATH, '//*[@id="theForm"]/div/table/tbody/tr[5]/td/input[1]'))

        #点击付款方式
        self.shop.click_distribution_1()
        #断言
        try:
            # 点击确定
            self.shop.click_invoicing()
            self.shop.click_window()
            self.assertTrue(True)
        except:
            self.assertTrue(False)
    def test_shopping_invoicing_052(self):
        # 点击视频商品
        self.shop.click_video()
        # 点击加入购物车
        self.shop.click_goods_confirm()
        #点击结账
        self.shop.click_goods_settlement()
        self.shop.click((By.XPATH, '//*[@id="theForm"]/div/table/tbody/tr[5]/td/input[1]'))
        #点击付款方式
        self.shop.click_distribution_2()
        #断言
        try:
            # 点击确定
            self.shop.click_invoicing()
            self.shop.click_window()
            self.assertTrue(True)
        except:
            self.assertTrue(False)

    def test_shopping_invoicing_053(self):
        # 点击视频商品
        self.shop.click_video()
        # 点击加入购物车
        self.shop.click_goods_confirm()
        # 点击结账
        self.shop.click_goods_settlement()
        self.shop.click((By.XPATH, '//*[@id="theForm"]/div/table/tbody/tr[5]/td/input[1]'))
        #点击配送方式
        self.shop.click_payment1()
        # 断言
        try:
            # 点击确定
            self.shop.click_invoicing()
            self.shop.click_window()
            self.assertTrue(True)
        except:
            self.assertTrue(False)
    def test_shopping_invoicing_054(self):
        # 点击视频商品
        self.shop.click_video()
        # 点击加入购物车
        self.shop.click_goods_confirm()
        # 点击结账
        self.shop.click_goods_settlement()
        self.shop.click((By.XPATH, '//*[@id="theForm"]/div/table/tbody/tr[5]/td/input[1]'))
        #点击配送方式
        self.shop.click_payment2()
        # 断言
        try:
            # 点击确定
            self.shop.click_invoicing()
            self.shop.click_window()
            self.assertTrue(True)
        except:
            self.assertTrue(False)
    def test_shopping_invoicing_055(self):
        # 点击视频商品
        self.shop.click_video()
        # 点击加入购物车
        self.shop.click_goods_confirm()
        # 点击结账
        self.shop.click_goods_settlement()
        self.shop.click((By.XPATH, '//*[@id="theForm"]/div/table/tbody/tr[5]/td/input[1]'))
        #付款方式
        self.shop.click_payment1()
        #配送方式
        self.shop.click_distribution_1()
        try:
            # 点击确定
            self.shop.click_invoicing()
            self.shop.click_window()
            self.assertTrue(True)
        except:
            self.assertTrue(False,msg="用例执行失败结算按钮点击后无弹框")
    def test_shopping_invoicing_056(self):
        # 点击视频商品
        self.shop.click_video()
        # 点击加入购物车
        self.shop.click_goods_confirm()
        # 点击结账
        self.shop.click_goods_settlement()
        self.shop.click((By.XPATH, '//*[@id="theForm"]/div/table/tbody/tr[5]/td/input[1]'))
        #付款方式
        self.shop.click_payment1()
        #配送方式
        self.shop.click_distribution_1()
        #点击确定
        self.shop.click_invoicing()
        #获取订单号
        order_number = self.shop.get_order_number()
        #点击用户中心
        self.shop.click_usercenter()
        #点击我的订单
        self.shop.click_myorder()
        #断言 判断是否有一致订单号
        self.assertEqual(order_number,self.shop.get_myorder(),"用例执行失败:无此订单")
